package com.seng.userservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seng.userservice.entity.TbUser;
import com.seng.userservice.mapper.TbUserMapper;
import com.seng.userservice.service.TbUserService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author seng
 * @since 2020-07-20
 */
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser> implements TbUserService {

    @Override
    public IPage<TbUser> findStudentPage(Page<TbUser> page, QueryWrapper<TbUser> wrapper) {
        return this.baseMapper.findStudentPage(page,wrapper);
    }
    /**
     * 导出学生信息excel
     *
     * @param response http响应
     */
    @Override
    public void exportStudentInfoExcel(HttpServletResponse response) throws IOException {
        // ----- 获取导出数据 -----
        List<TbUser> studentExcelInfo = this.baseMapper.selectList(null);

        //----- 写入excel文件
        String exportFileName = "TbUser.xlsx";
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.ms-excel");
        response.addHeader("Content-Disposition", "attachment;filename=" + exportFileName);
        EasyExcel.write(response.getOutputStream(), TbUser.class)
                .sheet("学生信息")
                //自动列宽
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                .doWrite(studentExcelInfo);
    }
}
